﻿using System;
using System.Collections.Generic;
using System.IO;
using ExcelBlockLib;

class Program
{
    static void Main(string[] args)
    {
        var newBlocks = new List<Dictionary<string, string>>
        {
            //new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-001", ["类型代号1"] = "G-001",["Revit材质"] = "透明玻璃" },
            //new Dictionary<string, string> { ["材质类别"] = "铝板", ["名称"] = "测试铝板01", ["类型代号"] = "ALP-001", ["颜色"] = "灰白", ["Revit材质"] = "粉末涂层" },
            //new Dictionary<string, string> { ["材质类别"] = "铝板", ["名称"] = "测试铝板03", ["类型代号"] = "ALP-002", ["颜色"] = "灰白", ["Revit材质"] = "粉末涂层" },
            //new Dictionary<string, string> { ["材质类别"] = "铝板", ["名称"] = "测试铝板03", ["类型代号"] = "ALP-002", ["颜色"] = "白色", ["Revit材质"] = "粉末涂层" },
            //new Dictionary<string, string> { ["材质类别"] = "辅材", ["名称"] = "测试辅材01", ["类型代号"] = "FM-001" },
            //new Dictionary<string, string> { ["材质类别"] = "辅材2", ["名称"] = "测试辅材02", ["类型代号"] = "FM-009", ["备注"] = "FM-009" },
            //new Dictionary<string, string> { ["材质类别"] = "辅材", ["名称"] = "测试辅材03", ["类型代号"] = "FM-002" },
            //new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃009", ["类型代号"] = "G-009", ["Revit材质"] = "透明玻璃" },
            // new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-004", ["类型代号1"] = "G-001",["Revit材质"] = "透明玻璃" },
            //  new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-0011", ["类型代号1"] = "G-001",["Revit材质"] = "透明玻璃" },
            //   new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-023", ["类型代号1"] = "G-001",["Revit材质"] = "透明玻璃" },
            //new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-111", ["Revit材质"] = "透明玻璃" },
            // new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-112", ["Revit材质"] = "透明玻璃" },
            //  new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃011111", ["类型代号"] = "G-113", ["Revit材质"] = "透明玻璃122222" },
            //   new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-114", ["Revit材质"] = "透明玻璃" },
            //    new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-115", ["Revit材质"] = "透明玻璃" },
            //new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-325", ["Revit材质"] = "透明玻璃" },
            // new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-325", ["Revit材质"] = "透明玻璃" },
            //new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-666", ["Revit材质"] = "透明玻璃" },
            //new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-777", ["Revit材质"] = "透明玻璃" },
            //new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-8", ["Revit材质"] = "透明玻璃" },
            //   new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-9", ["Revit材质"] = "透明玻璃" },
            //      new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-10", ["Revit材质"] = "透明玻璃" },
            //         new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-11", ["Revit材质"] = "透明玻璃", ["代号前缀"] = "透明玻璃"},
            //          new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-12", ["Revit材质"] = "透明玻璃",["111"] = "透明玻璃",["333"] = "透明玻璃", ["代号前缀"] = "透明玻璃"},
                       new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-13", ["Revit材质"] = "透明玻璃",["4444"] = "透明玻璃",["111"] = "透明玻璃",["333"] = "透明玻璃", ["代号前缀"] = "透明玻璃"},



        };

        var toDelete = new List<Dictionary<string, string>>
        {
                        //new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-8", ["Revit材质"] = "透明玻璃" },
                        //new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-10", ["Revit材质"] = "透明玻璃" },
            //new Dictionary<string, string> { ["材质类别"] = "玻璃", ["类型代号"] = "G-001" },
            //new Dictionary<string, string> { ["材质类别"] = "铝板", ["类型代号"] = "ALP-002" },
            // new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-112", ["Revit材质"] = "透明玻璃" },
            //          new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-113", ["Revit材质"] = "透明玻璃" },
            //new Dictionary<string, string> { ["材质类别"] = "铝板", ["类型代号"] = "ALP-001" },
            //new Dictionary<string, string> { ["材质类别"] = "辅材", ["类型代号"] = "FM-001" },
            //new Dictionary<string, string> { ["材质类别"] = "铝材", ["类型代号"] = "小件" },
            //new Dictionary<string, string> { ["材质类别"] = "铝材", ["类型代号"] = "集水槽" }
        };


        Console.WriteLine("📁 正在加载配置路径...");
        string importPath = @"D:\Desktop\mysql\空表.xlsx";
        string exportPath = @"D:\Desktop\mysql\空表.xlsx";

       
        string fieldDefPath = @"D:\Desktop\mysql\材料特性信息.xlsx"; // 修改为你本地实际路径

        ConfigManager.SetPath("Import", importPath);
        ConfigManager.SetPath("Export", exportPath);
        ConfigManager.SetPath("FieldDef", fieldDefPath);

        Console.WriteLine("📋 当前字段定义结构如下：");
        var fieldMap = typeof(ExcelBlockService)
            .GetField("MaterialFieldMap", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)
            ?.GetValue(null) as Dictionary<string, List<string>>;

        if (fieldMap != null)
        {
            foreach (var kv in fieldMap)
            {
                Console.WriteLine($"📄 {kv.Key}：{string.Join(", ", kv.Value)}");
            }
        }

        ExcelBlockService.GenerateEmptyExcelIfNotExist(importPath);

        Console.WriteLine("📥 读取数据文件: " + importPath);
        var data = ExcelBlockService.SafeLoad(importPath);
        Console.WriteLine("📊 已加载数据条数：" + data.Count);

        RunTestLogic(data, newBlocks, toDelete);

        Console.WriteLine("📤 正在导出第一次分类数据...");
        ExcelBlockService.ExportBlocksByCategory(exportPath, data);
        Console.WriteLine("✅ 第一次导出完成：" + exportPath);

        //【新增调试】打印实时导出字段顺序
        Console.WriteLine("📋 当前实时导出字段顺序：");
        foreach (var kv in ExcelBlockService.CurrentExportFieldMap)
        {
            Console.WriteLine($"📄 {kv.Key}：{string.Join(", ", kv.Value)}");
        }

        //【新增调试】导出后立即重新加载验证一次
        Console.WriteLine("🔄 正在重新加载导出的Excel文件以检查数据一致性...");
        var reloadedDataCheck = ExcelBlockService.SafeLoad(exportPath);
        Console.WriteLine("📋 重新加载的数据条数：" + reloadedDataCheck.Count);

        // 第二次导出
        string exportPathReloaded = Path.Combine(Path.GetDirectoryName(exportPath), "结果_重新载入导出.xlsx");
        Console.WriteLine("📤 正在导出第二次数据（带灰色）...");
        ExcelBlockService.ExportBlocksByCategory(exportPathReloaded, reloadedDataCheck);
        Console.WriteLine("✅ 第二次导出完成：" + exportPathReloaded);
        Console.WriteLine("🎉 全部完成，按任意键退出");
        Console.ReadKey();
    }

    static void RunTestLogic(List<Dictionary<string, string>> data, List<Dictionary<string, string>> newblocks, List<Dictionary<string, string>> toDelete)
    {
        //var newBlocks = new List<Dictionary<string, string>>
        //{
        //    //new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-001", ["类型代号1"] = "G-001",["Revit材质"] = "透明玻璃" },
        //    //new Dictionary<string, string> { ["材质类别"] = "铝板", ["名称"] = "测试铝板01", ["类型代号"] = "ALP-001", ["颜色"] = "灰白", ["Revit材质"] = "粉末涂层" },
        //    //new Dictionary<string, string> { ["材质类别"] = "铝板", ["名称"] = "测试铝板03", ["类型代号"] = "ALP-002", ["颜色"] = "灰白", ["Revit材质"] = "粉末涂层" },
        //    //new Dictionary<string, string> { ["材质类别"] = "铝板", ["名称"] = "测试铝板03", ["类型代号"] = "ALP-002", ["颜色"] = "白色", ["Revit材质"] = "粉末涂层" },
        //    //new Dictionary<string, string> { ["材质类别"] = "辅材", ["名称"] = "测试辅材01", ["类型代号"] = "FM-001" },
        //    //new Dictionary<string, string> { ["材质类别"] = "辅材2", ["名称"] = "测试辅材02", ["类型代号"] = "FM-009", ["备注"] = "FM-009" },
        //    //new Dictionary<string, string> { ["材质类别"] = "辅材", ["名称"] = "测试辅材03", ["类型代号"] = "FM-002" },
        //    //new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃009", ["类型代号"] = "G-009", ["Revit材质"] = "透明玻璃" },
        //    // new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-004", ["类型代号1"] = "G-001",["Revit材质"] = "透明玻璃" },
        //    //  new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-0011", ["类型代号1"] = "G-001",["Revit材质"] = "透明玻璃" },
        //    //   new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-023", ["类型代号1"] = "G-001",["Revit材质"] = "透明玻璃" },
        //    //new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-111", ["Revit材质"] = "透明玻璃" },
        //    // new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-112", ["Revit材质"] = "透明玻璃" },
        //    //  new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃011111", ["类型代号"] = "G-113", ["Revit材质"] = "透明玻璃122222" },
        //    //   new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-114", ["Revit材质"] = "透明玻璃" },
        //    //    new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-115", ["Revit材质"] = "透明玻璃" },
        //    //new Dictionary<string, string> { ["材质类别"] = "玻璃", ["名称"] = "测试玻璃01", ["类型代号"] = "G-325", ["Revit材质"] = "透明玻璃" },
        //    // new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-325", ["Revit材质"] = "透明玻璃" },
        //     //new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-666", ["Revit材质"] = "透明玻璃" },
        //     //new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-777", ["Revit材质"] = "透明玻璃" },
        //        //new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-8", ["Revit材质"] = "透明玻璃" },
        //        //   new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-9", ["Revit材质"] = "透明玻璃" },
        //        //      new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-10", ["Revit材质"] = "透明玻璃" },
        //        //         new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-11", ["Revit材质"] = "透明玻璃", ["代号前缀"] = "透明玻璃"},
        //        //          new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-12", ["Revit材质"] = "透明玻璃",["111"] = "透明玻璃",["333"] = "透明玻璃", ["代号前缀"] = "透明玻璃"},
        //                   new Dictionary<string, string> { ["材质类别"] = "铝材", ["名称"] = "测试玻璃01", ["类型代号"] = "G-13", ["Revit材质"] = "透明玻璃",["4444"] = "透明玻璃",["111"] = "透明玻璃",["333"] = "透明玻璃", ["代号前缀"] = "透明玻璃"},



        //};

        ExcelBlockService.AddOrUpdateBlocks(data, newblocks);
        Console.WriteLine("✅ 批量添加完成，共添 加或更新：" + newblocks.Count + " 条");

        var hBlock = new Dictionary<string, string>
        {
            ["材质类别"] = "铝材",
            ["名称"] = "H304636",
            ["类型代号"] = "集水槽",
            ["颜色"] = "1",
            ["面积"] = "/",
            ["线密度"] = "/",
            ["Revit材质"] = "/",
            ["项目"] = "已开摸",
            ["备注"] = "FM-009",
            ["生产厂家"] = "/"
        };
        //ExcelBlockService.AddOrUpdateBlock(data, hBlock);
        Console.WriteLine("✅ 单条数据添加完成");

        
        ExcelBlockService.MarkBlockAsDeleted(data, toDelete);

        Console.WriteLine("🗑 已标记逻辑删除条目数：" + toDelete.Count);


    }
}
